import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
public class StudentCreditManagementSystem {
    private static final Scanner scanner = new Scanner(System.in);
    private final java.util.List<Student> students = new ArrayList<>();
    public void showMenu() {
        boolean exit = false;
        while (!exit) {
            System.out.println("欢迎使用学生学分管理系统");
            System.out.println("1. 添加学生");
            System.out.println("2. 为学生添加课程成绩");
            System.out.println("3. 查询学生GPA");
            System.out.println("4. 立即导出学生选课记录及成绩（操作后实时导出）");
            System.out.println("5. 退出系统");
            System.out.print("请输入你的选择: ");
            int choice = scanner.nextInt();
            switch (choice) {
                case 1:
                    addStudent();
                    break;
                case 2:
                    addCourseGradeForStudent();
                    break;
                case 3:
                    queryStudentGPA();
                    break;
                case 4:
                    addCourseGradeForStudentWithExport(); // 修改，调用添加成绩并实时导出的新方法
                    break;
                case 5:
                    exit = true;
                    break;
                default:
                    System.out.println("无效的选择，请重新输入");
            }
        }
    }

    private void addStudent() {
        System.out.print("请输入学生学号: ");
        String studentId = scanner.next();
        System.out.print("请输入学生姓名: ");
        String studentName = scanner.next();
        Student student = new Student(studentId, studentName);
        students.add(student);
        System.out.println("学生添加成功");
    }
    private void addCourseGradeForStudent() {
        System.out.print("请输入学生学号: ");
        String studentId = scanner.next();
        Student student = findStudentById(studentId);
        if (student == null) {
            System.out.println("未找到该学号对应的学生，请重新输入");
            return;
        }
        System.out.print("请输入课程编号: ");
        String courseId = scanner.next();
        System.out.print("请输入课程名称: ");
        String courseName = scanner.next();
        System.out.print("请输入课程学分: ");
        int credit = scanner.nextInt();
        Course course = new Course(courseId, courseName, credit);
        System.out.print("请输入成绩（百分制）: ");
        double score = scanner.nextDouble();
        Grade courseGrade = new Grade(course, score);
        student.addCourseGrade(courseGrade);
        System.out.println("课程成绩添加成功");
    }


    private void addCourseGradeForStudentWithExport() {
        System.out.print("请输入学生学号: ");
        String studentId = scanner.next();
        Student student = findStudentById(studentId);
        if (student == null) {
            System.out.println("未找到该学号对应的学生，请重新输入");
            return;
        }
        exportStudentRecords(student.getStudentId(), getExportFilePath());
        System.out.println("课程成绩添加成功并已实时导出相关记录");
    }
    private Student findStudentById(String studentId) {
        for (Student student : students) {
            if (student.getStudentId().equals(studentId)) {
                return student;
            }
        }
        return null;
    }

    private void queryStudentGPA() {
        System.out.print("请输入学生学号: ");
        String studentId = scanner.next();
        Student student = findStudentById(studentId);
        if (student == null) {
            System.out.println("未找到该学号对应的学生，请重新输入");
            return;
        }
        double gpa = student.calculateGPA();
        System.out.println("学生 " + student.getStudentName() + " 的平均GPA为: " + gpa);
    }
    private void exportStudentRecords(String studentId, String filePath) {
        Student student = findStudentById(studentId);
        if (student == null) {
            System.out.println("未找到该学号对应的学生，请重新输入");
            return;
        }
        try (FileWriter writer = new FileWriter(filePath)) {
            writer.write("学生姓名: " + student.getStudentName() + "\n");
            writer.write("学号: " + student.getStudentId() + "\n");
            writer.write("所选课程及成绩:\n");
            for (Grade courseGrade : student.getCourseGrades()) {
                Course course = courseGrade.getCourse();
                double score = courseGrade.getScore();
                writer.write("课程编号: " + course.getCourseId() + ", 课程名称: " + course.getCourseName() +
                        ", 学分: " + course.credit() + ", 成绩: " + score + "\n");
                double gpa = student.calculateGPA();
                writer.write("平均学分绩点（GPA）: " + gpa + "\n");
            }

            System.out.println("学生选课记录及成绩已成功导出到 " + filePath);
        } catch (IOException e) {
            System.out.println("导出记录时出现错误: " + e.getMessage());
        }
    }
    // 获取导出文件路径的方法，可根据需要进一步完善输入提示等
    private String getExportFilePath() {
        System.out.print("填写路径");
        return scanner.next();
    }

    public static void main(String[] args) {
        StudentCreditManagementSystem system = new StudentCreditManagementSystem();
        system.showMenu();
    }
}